package dbutils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class Dutils_Demo2 {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
    @Test
    public void account() throws SQLException {

        QueryRunner qr = new QueryRunner();
        Connection con =null;
        try {
        con = dataSource.getConnection();
        //false 手动开启事务
            con.setAutoCommit(false);
            String sql = "update account set money=money-? where id =?";
            int row = qr.update(con, sql, 500, "2");

        if (row > 0){
            System.out.println("转账成功");
        }else {
            System.out.println("转账失败");
        }
            sql = "update account set money =money+? where id =?";
            int row1 = qr.update(con, sql, 500, "1");
            System.out.println(row1 > 0 ? "转入成功" : "转入失败");
            con.commit();
        } catch (Exception e) {
            con.rollback();
            e.printStackTrace();
        }
        finally {
            con.close();
        }
    }
}
